var mySqrt = function(x) {
    if(x==0) return 0
    if(x<4) return 1
    let l=1,
        r=x/2;
    while(r>=l){
        let mid=Math.floor((r+l)/2) 
        if(mid*mid<=x){
            if((mid+1)*(mid+1)>x){
                return mid
            }else{
                l=mid+1
            }
        }else{
            if((mid-1)*(mid-1)<x){
                return mid-1
            }
            r=mid-1
        }
    }
};
console.log(mySqrt(5));